package com.client;

import java.util.Date;
import java.util.List;

import com.shared.FieldVerifier;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.DatePicker;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestTimeZone implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while " + "attempting to contact the server. Please check your network " + "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

	public static String myTimeZone = "Asia/Calcutta";
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		utilDateTest(new Date());
		utilDateTest(new Date(91,0,1,04,20,20));
		utilDateTest(new Date(111,1,1,10,0,0));
		utilDateTest(new Date(111,4,1,10,0,0));
		utilDateTest(new Date(105,11,10,20,10,10));
		
		dateFieldTest();
		
//		sqlTimestampTest();
//		sqlDateTest();
		
		GridExample grid = new GridExample();
		RootPanel.get().add(grid);
		
		
		greetingService.greetServer(new AsyncCallback<List<TestDomain>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<TestDomain> result) {
				// TODO Auto-generated method stub
				
//				Window.alert(result.size() + "");
			}
			
		});
		
		DatePicker dp = new DatePicker();
		RootPanel.get().add(dp);
	}
	
	public void utilDateTest(Date d) {
		Label label1 = new Label("util.date; " + d.toString() + "--- millsec:" + d.getTime());
		RootPanel.get().add(label1);

		final TextArea textArea = new TextArea();
		RootPanel.get().add(textArea);
		
		greetingService.greetServer(d, new AsyncCallback<Date>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub				
			}

			@Override
			public void onSuccess(Date d) {
				textArea.setText(d.toString());
				
			}
		});				
	}

	public void dateFieldTest() {
		
		DateTimeFormat dtFormat = DateTimeFormat.getFormat("dd-MM-yyyy hh:mm:ss");		
		
		final DateField dateField = new DateField();
		dateField.setValue(new Date());
		dateField.getPropertyEditor().setFormat(dtFormat);
		
		RootPanel.get().add(dateField);
		
		greetingService.greetServer(dateField.getValue(), new AsyncCallback<Date>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub				
			}

			@Override
			public void onSuccess(Date d) {
				dateField.setValue(d);
			}
		});				
		
		
		Button button = new Button("Submit DateField", new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.alert("event occured");
				greetingService.greetServer(dateField.getValue(), new AsyncCallback<Date>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub				
					}

					@Override
					public void onSuccess(Date d) {
						dateField.setValue(d);
					}
				});				
			}
		});

		RootPanel.get().add(button);
	}
	
	public void sqlTimestampTest() {
		Label label2 = new Label("sql.timestamp; " + new java.sql.Timestamp(new Date().getTime()).toString());
		RootPanel.get().add(label2);

		final TextArea textArea = new TextArea();
		RootPanel.get().add(textArea);
		
		greetingService.greetServer(new Date(), new AsyncCallback<Date>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub				
			}

			@Override
			public void onSuccess(Date d) {
				textArea.setText(d.toString());
				
			}
		});		
	}
	
	public void sqlDateTest() {
		Label label3 = new Label("sql.date; " + new java.sql.Date(85,11,23).toString());
		RootPanel.get().add(label3);

		final TextArea textArea = new TextArea();
		RootPanel.get().add(textArea);
		
		greetingService.greetServer(new Date(), new AsyncCallback<Date>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub				
			}

			@Override
			public void onSuccess(Date d) {
				textArea.setText(d.toString());
				
			}
		});		
		
		
		
	}
	
}
